import 'package:my_flutter_core/models/user.dart';
import 'package:my_flutter_core/services/route_map_config_service.dart';
import 'package:my_flutter_core/services/route_map_manager.dart';
import 'package:my_flutter_core/utils/ui_interaction_utils.dart';

/// Sets route mapping config
///
/// Example:
///
/// // Router navigation supports deep linking.
// ignore: unintended_html_in_doc_comment
/// static final Map<String, String> routeConfig = {
///   // region: The configuration allows anonymous access.
///   '/startup-view': RouteMapManager.permitAll,
///   '/authentication/**': RouteMapManager.permitAll,
///   '/system-setting/app-permission-setting': RouteMapManager.permitAll,
///   // endregion
///
///   // region: The configuration allows authenticated access.
///   '/system-setting/**': RouteMapManager.isAuthenticated,
///   '/account-user/**': RouteMapManager.isAuthenticated,
///   '/bank-card/**': RouteMapManager.isAuthenticated,
///   '/credit-card-bill/**': RouteMapManager.isAuthenticated,
///   '/income-expense/**': RouteMapManager.isAuthenticated,
///   '/income-expense-analysis/**': RouteMapManager.isAuthenticated,
///   '/income-expense-detail-type/**': RouteMapManager.isAuthenticated,
///   '/income-expense-search/**': RouteMapManager.isAuthenticated,
///   '/journey/**': RouteMapManager.isAuthenticated,
///   // endregion
///
///   // region: The configuration only allows account group user and admin access.
///   '/income-expense/edit': RouteMapManager.hasAnyRole(
///       [UserRole.accountGroupUser, UserRole.accountGroupAdmin]),
///   '/journey/edit': RouteMapManager.hasAnyRole(
///       [UserRole.accountGroupUser, UserRole.accountGroupAdmin]),
///   '/journey/income-expense/edit': RouteMapManager.hasAnyRole(
///       [UserRole.accountGroupUser, UserRole.accountGroupAdmin]),
///   // endregion
///
///   // region: The configuration allows account group admin access.
///   '/account-user/add': RouteMapManager.hasRole(UserRole.accountGroupAdmin),
///   '/bank-card/add': RouteMapManager.hasRole(UserRole.accountGroupAdmin),
///   '/income-expense-detail-type/edit':
///       RouteMapManager.hasRole(UserRole.accountGroupAdmin),
///   '/account-user/update': RouteMapManager.hasRole(UserRole.accountGroupAdmin),
///   // endregion
/// };
///
/// Sets in your main.dart.
///
/// RouteMapConfigService.getRouteConfigAttribute(RouteMapConfig.routeConfig);
///
/// Uses route check in your GoRouter root redirect setting.
///
/// Example:
///
/// redirect: (context, state) async {
///   final user = await AuthenticationService.currentUser;
///
///   final canActivate =
///       RouterHelper.checkCanActivate(state.matchedLocation, user: user);
///
///   return canActivate ? null : '/authentication/password-login';
/// },
class RouterGuard {
  /// Checks if the provided route can be activated.
  /// The route can be activated only when it is open route or current user is authenticated.
  static bool checkCanActivate(String route, {User? user}) {
    // Gets route map config by route name.
    final routeConfigs = RouteMapConfigService.getRouteConfigAttribute();

    // Validates route access permission.
    var canActivate = RouteMapManager.checkRouteOnConfigAttributes(
      user,
      route,
      routeConfigs,
    );

    if (!canActivate) {
      showToast('对不起，您当前账号没有权限使用该功能，您可以尝试以其它账号重新登录。');
    }

    return canActivate;
  }
}
